//
//  SystemNewsCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/20.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SystemNewsCtrl.h"
#import "MyAddTableViewCell.h"
#import "SystemNewsViewCell.h"
#import "MyAddModel.h"
#import "SpacePagerModel.h"
#import "SeleCtContrl.h"
#import "SysModel.h"
#import "OtherInformationCtrl.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface SystemNewsCtrl ()<UITableViewDelegate,UITableViewDataSource,ForMyDeleget>
@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,assign) CGFloat cellHight;
@property (nonatomic,strong) SysModel *model;
@property (nonatomic,strong) NSMutableArray *dataAry;
@property (nonatomic,assign) NSInteger nextpage;
@end

@implementation SystemNewsCtrl
-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"SystemNewsViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    [self.view addSubview:self.tableview];
    self.tableview.separatorStyle = 0;
    self.tableview.backgroundColor = tabPlainBackColor;
    
    self.tableview.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshFooter)];
    
    self.nextpage = 2;
    
    [self AchiveDataWhithPage:1];
    __weak typeof(self) weakself = self;
    _tableview.TapBlok = ^{ [weakself AchiveDataWhithPage:1]; };

}

-(void)RefreshFooter{
    
    SpacePagerModel *pageModel = self.model.pager;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.tableview.footer noticeNoMoreData];
    }else
        [self AchiveDataWhithPage:self.nextpage];
    self.nextpage ++;
}


-(void)AchiveDataWhithPage:(NSInteger)page{
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:SysteNews,(long)page] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if (error) {
            self.tableview.isShowFile = YES;
            [self.tableview reloadData];
            
            [_tableview.footer endRefreshing];
        }else{
        self.model = [[SysModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        
        [self.dataAry addObjectsFromArray:self.model.list];
        [self.tableview reloadData];
        [self.tableview.footer endRefreshing];
        
        self.tableview.isShowImage = YES;
        [self.tableview reloadData];
        
        [_tableview.footer endRefreshing];
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.cellHight+0.1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SystemNewsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.model = self.dataAry[indexPath.row];
    self.cellHight = cell.cellHight;
    cell.deleget = self;
    return cell;
}

-(void)SpaceActionWhithId:(NSString *)uid{
    
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.navigationController pushViewController:space animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SystemNewsViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SeleCtContrl *str = [[SeleCtContrl alloc] init];
    
    str.tid = cell.tid;
    str.url = [NSString stringWithFormat:MainSelet,cell.tid];
    [self.navigationController pushViewController:str animated:YES];
}
@end